package it.yuzuojian.com.controller;

import it.yuzuojian.com.service.GeneratorService;
import it.yuzuojian.com.service.impl.GeneratorServiceImpl;
import it.yuzuojian.com.util.DataSourceUtil;
import lombok.Data;
import lombok.SneakyThrows;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

@Data
@Mojo(name = "CodeGenerator" ,requiresDependencyResolution= ResolutionScope.RUNTIME
        ,defaultPhase = LifecyclePhase.PREPARE_PACKAGE
        )

public class GenerateController extends AbstractMojo {
    @Parameter(name = "name", defaultValue = "lisi")
    private String name;

    @Parameter(name = "address", property = "address", defaultValue = "上海")
    private String address;

    @Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
    private  String buildDirectory;
    @Parameter(defaultValue = "${project.build.sourceDirectory}", readonly = true, required = true)
    private  String sourceDirectory;
    @Parameter(defaultValue = "${basedir}", readonly = true, required = true)
    private  String basedir;
    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject mavenProject ;
    @SneakyThrows
    @Override
    public void execute() {
        List<Resource> resources = mavenProject.getResources();
        DataSourceUtil.resourcesDirectory=resources.get(0).getDirectory();
        DataSourceUtil.sourceDirectory=sourceDirectory;
        GeneratorService generatorService = new GeneratorServiceImpl();
        InputStream inputStream = new FileInputStream(DataSourceUtil.resourcesDirectory+ File.separator+"generator.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String database = properties.getProperty("database");
        String[] tableNames = generatorService.getAllTable(database);

        try {
            generatorService.generatorCode(tableNames);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /*@RequestMapping("/index")
    String welcome() {
        return "index";
    }

    @RequestMapping("/batchCode")//http://localhost:8009/batchCode
    public void batchCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] tableNames = new String[] {"student"};
        byte[] data = generatorService.generatorCodeWeb(tableNames);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"bootdo.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(data);
        //IOUtils.write(data, response.getOutputStream());
    }
    @RequestMapping("/batchCode_")//http://localhost:8009/batchCode_
    public void batchCode_(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] tableNames = new String[] {"student"};
        generatorService.generatorCode(tableNames);

    }*/
}

